package cn.iocoder.an.module.business.controller.admin.filedaily;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.filedaily.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.filedaily.FileDailyDO;
import cn.iocoder.an.module.business.service.filedaily.FileDailyService;

@Tag(name = "管理后台 - 报")
@RestController
@RequestMapping("/business/file-daily")
@Validated
public class FileDailyController {

    @Resource
    private FileDailyService fileDailyService;

    @PostMapping("/create")
    @Operation(summary = "创建报")
    @PreAuthorize("@ss.hasPermission('business:file-daily:create')")
    public CommonResult<String> createFileDaily(@Valid @RequestBody FileDailySaveReqVO createReqVO) {
        return success(fileDailyService.createFileDaily(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报")
    @PreAuthorize("@ss.hasPermission('business:file-daily:update')")
    public CommonResult<Boolean> updateFileDaily(@Valid @RequestBody FileDailySaveReqVO updateReqVO) {
        fileDailyService.updateFileDaily(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:file-daily:delete')")
    public CommonResult<Boolean> deleteFileDaily(@RequestParam("id") String id) {
        fileDailyService.deleteFileDaily(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报")
                @PreAuthorize("@ss.hasPermission('business:file-daily:delete')")
    public CommonResult<Boolean> deleteFileDailyList(@RequestParam("ids") List<LocalDate> ids) {
        fileDailyService.deleteFileDailyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:file-daily:query')")
    public CommonResult<FileDailyRespVO> getFileDaily(@RequestParam("id") String id) {
        FileDailyDO fileDaily = fileDailyService.getFileDaily(id);
        return success(BeanUtils.toBean(fileDaily, FileDailyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报分页")
    @PreAuthorize("@ss.hasPermission('business:file-daily:query')")
    public CommonResult<PageResult<FileDailyRespVO>> getFileDailyPage(@Valid FileDailyPageReqVO pageReqVO) {
        PageResult<FileDailyDO> pageResult = fileDailyService.getFileDailyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileDailyRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得报分页")
    @PreAuthorize("@ss.hasPermission('business:file-daily:query')")
    public CommonResult<List<FileDailyRespVO>> list() {
        List<FileDailyDO> pageResult = fileDailyService.getList();
        return success(BeanUtils.toBean(pageResult, FileDailyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报 Excel")
    @PreAuthorize("@ss.hasPermission('business:file-daily:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileDailyExcel(@Valid FileDailyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FileDailyDO> list = fileDailyService.getFileDailyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报.xls", "数据", FileDailyRespVO.class,
                        BeanUtils.toBean(list, FileDailyRespVO.class));
    }

}